<html>
<head><meta charset="utf-8"><title>Writing pretty printers in Rust · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html">Writing pretty printers in Rust</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219707040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707040">(Dec 12 2020 at 12:40)</a>:</h4>
<p>What's the 'best' way to write pretty printers in Rust? Is it really by combining calls to <code>write!</code>?  The format string approach seems to fail if you want to make each substitution a sub-printer itself. Also, what about handling things like line wrapping? In haskell there are packages like <code>prettyprinter</code> which allow you to print to an AST which is then rendered to a specific line width. This approach allows you to easily compose printers and separate concerns about output formats from the printer shape. Are there any crates like this in Rust?</p>



<a name="219707307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707307">(Dec 12 2020 at 12:47)</a>:</h4>
<p>I used the <a href="https://crates.io/crates/pretty"><code>pretty</code></a> crate when porting some pretty printing code from haskell fairly recently</p>



<a name="219707314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707314">(Dec 12 2020 at 12:47)</a>:</h4>
<p>I saw that crate, I'm thinking of using it, I'm just surprised I haven't found a more 'blessed' pattern</p>



<a name="219707361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707361">(Dec 12 2020 at 12:48)</a>:</h4>
<p>surely this is a common problem? Or does everyone 'just deal with it'?</p>



<a name="219707375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707375">(Dec 12 2020 at 12:48)</a>:</h4>
<p>You can use Display for pretty printing, you just need a wrapper struct that contains a pointer to your pretty printing context</p>



<a name="219707395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707395">(Dec 12 2020 at 12:49)</a>:</h4>
<p>but for wadler style pretty printing you basically have to first preallocate and measure the groups you intend to print, so it can't be done in a very streaming way</p>



<a name="219707437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707437">(Dec 12 2020 at 12:50)</a>:</h4>
<p>yea but my point is that Display is done by manually writing to a Formatter</p>



<a name="219707468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707468">(Dec 12 2020 at 12:51)</a>:</h4>
<p>Fair point wrt streaming, but with dispaly printers it becomes hard to do something as simple as "indent this sub-value by 2 spaces"</p>



<a name="219707475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707475">(Dec 12 2020 at 12:51)</a>:</h4>
<p>That's what the wrapper struct is for</p>



<a name="219707516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707516">(Dec 12 2020 at 12:52)</a>:</h4>
<p>yea even then, it doesn't help much</p>



<a name="219707518"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707518" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707518">(Dec 12 2020 at 12:52)</a>:</h4>
<p>unless you have entirely custom printing macros</p>



<a name="219707528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707528">(Dec 12 2020 at 12:52)</a>:</h4>
<p>which i guess.. wouldn't be that hard</p>



<a name="219707540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707540">(Dec 12 2020 at 12:53)</a>:</h4>
<p>I'm just surprised since usually there's a great crate for things in Rust</p>



<a name="219707546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707546">(Dec 12 2020 at 12:53)</a>:</h4>
<p>Here's an example from my project: <a href="https://github.com/digama0/mm0/blob/master/mm0-rs/src/elab/lisp/print.rs">https://github.com/digama0/mm0/blob/master/mm0-rs/src/elab/lisp/print.rs</a></p>



<a name="219707603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707603">(Dec 12 2020 at 12:54)</a>:</h4>
<p>It doesn't use custom macros, instead you use regular <code>write!</code> to a formatter but it uses the <code>Print</code> wrapper struct to provide additional context that is not provided by <code>format</code></p>



<a name="219707621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707621">(Dec 12 2020 at 12:55)</a>:</h4>
<p>and in recursive calls you just wrap subobjects in the wrapper and use them in <code>write!</code> like normal</p>



<a name="219707674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707674">(Dec 12 2020 at 12:56)</a>:</h4>
<p>This is sort of the low tech solution, which is enough for tracking indentation, but for proper pretty printing, where you want to use line breaks only when necessary, something like the pretty crate is more appropriate</p>



<a name="219707739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707739">(Dec 12 2020 at 12:58)</a>:</h4>
<p>I don't know if there is a more 'blessed' solution, but this one worked well for me. I would be curious to hear if others have had their own pretty printing experiences</p>



<a name="219707794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707794">(Dec 12 2020 at 13:00)</a>:</h4>
<p>ok i see how your setup works</p>



<a name="219707845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707845">(Dec 12 2020 at 13:00)</a>:</h4>
<p>I was messing around with a more rudimentary version of that by wrapping a <code>Formatter</code> before but it didn't work well</p>



<a name="219707863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707863">(Dec 12 2020 at 13:00)</a>:</h4>
<p>because if I had something like <code>write!(f, "my cool \n string")</code> I wanted both liens to have the same indentation</p>



<a name="219707932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707932">(Dec 12 2020 at 13:02)</a>:</h4>
<p>if you want that to work out of the box you would have to analyze the string for newlines, right? I think the pretty crate bans newlines in text nodes and just requires that you construct them separately</p>



<a name="219707934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707934">(Dec 12 2020 at 13:02)</a>:</h4>
<p>though I had a different approach, rather than wrapping objects I had a method that took a closure</p>



<a name="219707944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707944">(Dec 12 2020 at 13:02)</a>:</h4>
<p>yea that makes sense</p>



<a name="219707960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219707960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219707960">(Dec 12 2020 at 13:03)</a>:</h4>
<p>of course you could probably do that at compile time with a fancy enough macro</p>



<a name="219708038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708038">(Dec 12 2020 at 13:04)</a>:</h4>
<p>hmm well I'll try to fix up my indentation aware printer,  see how far I get with that</p>



<a name="219708046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708046">(Dec 12 2020 at 13:05)</a>:</h4>
<p>I'm still surprised that there isn't a more elegant rusty solution though</p>



<a name="219708059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708059">(Dec 12 2020 at 13:05)</a>:</h4>
<p>maybe I'll write a crazy printer macro :P</p>



<a name="219708615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708615">(Dec 12 2020 at 13:19)</a>:</h4>
<p>oh right! I just remembered the _real_ reason I struggled, using <code>write</code> means that often you break up what is one line of output into multiple calls to <code>write!</code> (for example to loop over a list of things)</p>



<a name="219708624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708624">(Dec 12 2020 at 13:19)</a>:</h4>
<p>which means the printer needs to know when an _output_ line starts</p>



<a name="219708677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708677">(Dec 12 2020 at 13:20)</a>:</h4>
<p>eh... I'll have to figure someting out</p>



<a name="219708681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708681">(Dec 12 2020 at 13:20)</a>:</h4>
<p>you can just have a function <code>write_line()</code>, right?</p>



<a name="219708698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708698">(Dec 12 2020 at 13:21)</a>:</h4>
<p>yea, of course, I'd just like to have the least error-prone version :P</p>



<a name="219708703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708703">(Dec 12 2020 at 13:21)</a>:</h4>
<p>or if you are being fancy and parsing the input, you can just look for newlines in the string and use <code>writeln!</code></p>



<a name="219708712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708712">(Dec 12 2020 at 13:21)</a>:</h4>
<p>rn I'm actually thinking of replacing <code>writeln!</code> so it outputs the correct indentation after the newline</p>



<a name="219708763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708763">(Dec 12 2020 at 13:22)</a>:</h4>
<p>that works as long as your last line has indentation 0</p>



<a name="219708768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708768">(Dec 12 2020 at 13:22)</a>:</h4>
<p>yea</p>



<a name="219708797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708797">(Dec 12 2020 at 13:23)</a>:</h4>
<p>for reference the problem is code like this:</p>
<div class="codehilite"><pre><span></span><code>    write!(p, &quot;let cfg {} &quot;, func.name)?;
    for (nm, ty) in &amp;func.args {
        write!(p, &quot;(o_{} : {})&quot;, nm, ty)?;
    }
</code></pre></div>
<p>where they are all the same line (or should be)</p>



<a name="219708806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708806">(Dec 12 2020 at 13:23)</a>:</h4>
<p>where are the newlines?</p>



<a name="219708846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708846">(Dec 12 2020 at 13:24)</a>:</h4>
<p>none there, that's the whole point</p>



<a name="219708849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708849">(Dec 12 2020 at 13:24)</a>:</h4>
<p>here's a more complete example:</p>
<div class="codehilite"><pre><span></span><code>   write!(p, &quot;let cfg {} &quot;, func.name)?;

    for (nm, ty) in &amp;func.args {
        write!(p, &quot;(o_{} : {})&quot;, nm, ty)?;
    }

    writeln!(p, &quot; : {}&quot;, func.retty)?;

    for req in &amp;func.preconds {
        writeln!(p, &quot;requires {{ {} }}&quot;, req)?;
    }
    for req in &amp;func.postconds {
        writeln!(p, &quot;ensures {{ {} }}&quot;, req)?;
    }
</code></pre></div>



<a name="219708851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708851">(Dec 12 2020 at 13:24)</a>:</h4>
<p>so you want that to break once the line gets long enough, with indentation?</p>



<a name="219708854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708854">(Dec 12 2020 at 13:24)</a>:</h4>
<p>oh no, right now I'm just going to do indentation</p>



<a name="219708858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708858">(Dec 12 2020 at 13:24)</a>:</h4>
<p>idc about line length, for that I'd need <code>pretty</code> or something alogn those lines</p>



<a name="219708866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708866">(Dec 12 2020 at 13:25)</a>:</h4>
<p>I thought I had a clever hack by implementing <code>write_fmt</code> to prefix lines with indentation</p>



<a name="219708888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708888">(Dec 12 2020 at 13:26)</a>:</h4>
<p>I'm currently thinking I'll just check if the last <code>Arguments</code> is a newline and use that</p>



<a name="219708927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708927">(Dec 12 2020 at 13:26)</a>:</h4>
<p>it'll be good enough for now and definitely better than the current code</p>



<a name="219708944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708944">(Dec 12 2020 at 13:27)</a>:</h4>
<p>is that how <code>writeln!</code> works? I could imagine it either editing the format literal to add a <code>"\n"</code> or interpolating one in</p>



<a name="219708954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708954">(Dec 12 2020 at 13:27)</a>:</h4>
<p>It just adds the string literal <code>"\n"</code> to the list of expressions to print</p>



<a name="219708996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219708996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219708996">(Dec 12 2020 at 13:28)</a>:</h4>
<p><a href="https://doc.rust-lang.org/src/core/macros/mod.rs.html#454">https://doc.rust-lang.org/src/core/macros/mod.rs.html#454</a></p>



<a name="219709011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709011">(Dec 12 2020 at 13:28)</a>:</h4>
<p>maybe <code>format_args!</code> does some magic to it after</p>



<a name="219709030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709030">(Dec 12 2020 at 13:29)</a>:</h4>
<p>ooh, <code>format_args_nl</code> looks like some more magic</p>



<a name="219709035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709035">(Dec 12 2020 at 13:29)</a>:</h4>
<div class="codehilite"><pre><span></span><code>macro_rules! format_args {
        ($fmt:expr) =&gt; {{ /* compiler built-in */ }};
        ($fmt:expr, $($args:tt)*) =&gt; {{ /* compiler built-in */ }};
    }
</code></pre></div>
<p>ah nice</p>



<a name="219709036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709036">(Dec 12 2020 at 13:29)</a>:</h4>
<p>the version that adds <code>"\n"</code> only triggers if you aren't interpolating</p>



<a name="219709079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709079">(Dec 12 2020 at 13:30)</a>:</h4>
<p>yea</p>



<a name="219709081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709081">(Dec 12 2020 at 13:30)</a>:</h4>
<p>time to pull up rustc :)))))</p>



<a name="219709097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709097">(Dec 12 2020 at 13:31)</a>:</h4>
<div class="codehilite"><pre><span></span><code> Ok(mut fmt) if append_newline =&gt; {
            fmt.0 = Symbol::intern(&amp;format!(&quot;{}\n&quot;, fmt.0));
            fmt
        }
</code></pre></div>



<a name="219709098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709098">(Dec 12 2020 at 13:31)</a>:</h4>
<p>the relevant bit in <code>rustc</code></p>



<a name="219709172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709172">(Dec 12 2020 at 13:33)</a>:</h4>
<p>so seems like it should suffice to check if the last format string has a <code>\n</code></p>



<a name="219709232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709232">(Dec 12 2020 at 13:34)</a>:</h4>
<p>ah its all private :(</p>



<a name="219709235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709235">(Dec 12 2020 at 13:34)</a>:</h4>
<p><code>Arguments</code> you mean?</p>



<a name="219709240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709240">(Dec 12 2020 at 13:34)</a>:</h4>
<p>yea</p>



<a name="219709305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/219709305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#219709305">(Dec 12 2020 at 13:36)</a>:</h4>
<p>I'll find another awful hack :)</p>



<a name="220011122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/220011122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#220011122">(Dec 15 2020 at 17:14)</a>:</h4>
<p><span class="user-mention" data-user-id="312719">@Xavier Denis</span> Do you already know about the debug helpers like debug_map? Those seem close to what you're doing, and they handle indented pretty-printing.</p>



<a name="220011330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/220011330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#220011330">(Dec 15 2020 at 17:16)</a>:</h4>
<p>Yea that might be a good idea for inspiration on the api</p>



<a name="220011362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/220011362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#220011362">(Dec 15 2020 at 17:16)</a>:</h4>
<p>though i'm not formatting a map (but rather expressions in a language)</p>



<a name="220011435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/220011435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#220011435">(Dec 15 2020 at 17:16)</a>:</h4>
<p>I ended up going with the wrapping-struct idea of Mario though I'm not super happy about it, it still feels verbose</p>



<a name="220011458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Writing%20pretty%20printers%20in%20Rust/near/220011458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Writing.20pretty.20printers.20in.20Rust.html#220011458">(Dec 15 2020 at 17:17)</a>:</h4>
<p>its definitely better than what I had before</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>